using System;

namespace Framework.Library.Core.Utils.Miscellaneous
{
    public static class CurrencyUtils
    {
        public const string ValidateDollarString = @"(^\$?(?!0,?\d)\d{1,3}(,?\d{3})*(\.\d\d)?)$";

        /// <summary>
        /// Converts to decimal.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static decimal ConvertToDecimal(object value)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            if (value is decimal)
                return (decimal) value;
            else
                return ConvertToDecimal(value.ToString());
        }

        /// <summary>
        /// Converts to decimal.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        public static decimal ConvertToDecimal(string value)
        {
            ValidationUtils.ArgumentNotNullOrEmpty(value, "value");

            // remove whitespace
            string trimmedValue = value.Trim();
            // remove leading $
            trimmedValue = trimmedValue.TrimStart('$');

            return Convert.ToDecimal(trimmedValue);
        }
    }
}